/**
 * Created by wfle on 2018/5/10.
 */
import { GLOBAL } from '../config/global';
import { API, API_GENERATE } from '../config/api';
import HTTP from './http'

class Upload {

  /* 图片上传 */
  async uploadImg (options, config) {
    let formData = await this.getRequestParams(options);
    return HTTP.request(API_GENERATE(API.IMG_UPLOAD), 'POST', formData, config, {
      'Content-Type': 'multipart/form-data',
      "authToken": GLOBAL.TOKEN || ''
    }, 30000)
  }

  async getRequestParams (options) {
    /* options = {
        imgUrl: { uri: 'http:www.baidu.com/a.jpg' },  //本地图片地址
        url: '', //上传到哪里
      }
    } */
    let formData = new FormData();
    let file = { ...options.imgUrl, type: 'multipart/form-data;', name: "file.jpg" };   //这里的key(uri和type和name)不能改变,
    formData.append("file", file);   //这里的files就是后台需要的key
    if (options.isOcr) {
      formData.append("isOcr", options.isOcr);
      formData.append("ocrType", options.ocrType);
    }
    return formData;
  }

}

module.exports = new Upload();